/*
 * Copyright ©2015-2021 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.jaemon.spring.adorner.extend;

import io.gitee.jaemon.spring.adorner.core.AdornerApplicationContext;
import io.gitee.jaemon.spring.adorner.exception.AdornerException;
import io.gitee.jaemon.spring.adorner.support.AdornerNoteScanner;
import io.gitee.jaemon.spring.adorner.util.ClassUtils;
import io.gitee.jaemon.spring.adorner.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义注释扫描器
 *
 * <p>
 *     EnhanceNoteScanner注释扫描器是在Adorner基础扫描器的基础上进行增强
 * </p>
 *
 * @author Jaemon
 * @since 1.0
 */
public class EnhanceNoteScanner extends AdornerNoteScanner {

    @Override
    public boolean doScan(AdornerApplicationContext context) throws AdornerException {
        boolean flag = super.doScan(context);

        List<String> packages = context.docScanner().getPackages();
        List<Class<?>> classes = new ArrayList<>();
        ClassUtils.doScan(classes, packages);

        for (Class<?> clazz : classes) {
            String className = clazz.getName();
            // 解析controller注释
            if (clazz.isAnnotationPresent(Note.class)) {
                Note annotation = clazz.getAnnotation(Note.class);
                if (StringUtils.isNotEmpty(annotation.value())) {
                    classPut(className, annotation.value());
                }
            }

            // 解析api接口注释
            Method[] declaredMethods = clazz.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                String methodName = declaredMethod.getName();
                if (declaredMethod.isAnnotationPresent(Note.class)) {
                    Note annotation = declaredMethod.getAnnotation(Note.class);
                    if (StringUtils.isNotEmpty(annotation.value())) {
                        methodPut(className + ClassUtils.SPOT + methodName, annotation.value());
                    }
                }
            }

            // 解析字段注释
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                String fieldName = declaredField.getName();
                if (declaredField.isAnnotationPresent(Note.class)) {
                    Note annotation = declaredField.getAnnotation(Note.class);
                    if (StringUtils.isNotEmpty(annotation.value())) {
                        boolean required = annotation.required();
                        fieldPut(className + ClassUtils.SPOT + fieldName, annotation.value() + (required ? "" : "可选"));
                    }
                }
            }

        }

        return flag;
    }
}